package com.cleverest.cleverestdiary.controller;

import com.cleverest.cleverestdiary.pojo.PageBean;
import com.cleverest.cleverestdiary.pojo.Result;
import com.cleverest.cleverestdiary.pojo.Schedule;
import com.cleverest.cleverestdiary.service.ScheduleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * 日程相关的控制器，处理与日程相关的HTTP请求
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 新增日程
     *
     * @param schedule 要新增的日程对象
     * @return 操作结果
     */
    @PostMapping
    public Result add(@RequestBody @Validated(Schedule.Add.class) Schedule schedule) {
        scheduleService.add(schedule);
        return Result.success();
    }

    /**
     * 获取文章列表
     *
     * @param pageNum    页码，用于分页查询
     * @param pageSize   每页数量，用于分页查询
     * @param categoryId 日程分类 ID，可选参数
     * @param state      日程状态，可选参数
     * @param keyword    搜索关键字，可选参数
     * @return 分页后的日程列表，使用 Result 类封装，其中包含 PageBean<Article> 对象
     */
    @GetMapping
    public Result<PageBean<Schedule>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) Integer state,
            @RequestParam(required = false) Integer emergencyDegree,
            @RequestParam(required = false) String keyword
    ) {
        PageBean<Schedule> pb = scheduleService.list(pageNum, pageSize, categoryId, state, emergencyDegree, keyword);
        return Result.success(pb);
    }

    /**
     * 根据ID获取日程详情
     *
     * @param id 日程ID
     * @return 日程详情
     */
    @GetMapping("/detail")
    public Result<Schedule> detail(Integer id) {
        Schedule schedule = scheduleService.findById(id);
        return Result.success(schedule);
    }

    /**
     * 更新日程信息
     *
     * @param schedule 要更新的日程对象
     * @return 操作结果
     */
    @PutMapping
    public Result update(@RequestBody @Validated(Schedule.Update.class) Schedule schedule) {
        scheduleService.update(schedule);
        return Result.success();
    }

    /**
     * 删除日程
     *
     * @param id 要删除的日程ID
     * @return 操作结果
     */
    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        Schedule schedule = scheduleService.findById(id);
        if (schedule == null) {
            return Result.error("找不到这个日程");
        } else {
            scheduleService.delete(id);
        }
        return Result.success();
    }

    /**
     * 根据关键字搜索日程
     *
     * @param keyword  搜索关键字
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @return 搜索结果
     */
    @GetMapping("/search")
    public Result<PageBean<Schedule>> searchByKeyword(
            @RequestParam String keyword,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) {
        PageBean<Schedule> pb = scheduleService.searchByKeyword(pageNum, pageSize, keyword);
        return Result.success(pb);
    }

    /**
     * 更新日程状态信息，使得日程变成“已完成”
     *
     * @param schedule 要更新的日程对象
     * @return 操作结果
     */
    @PutMapping("/updateState")
    public Result updateState(@RequestBody @Validated(Schedule.Update.class) Schedule schedule) {
        scheduleService.updateState(schedule);
        return Result.success();
    }

    /**
     * 筛选某一天的所有日程
     * @param date 筛选的日期，格式为 yyyy-MM-dd
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 筛选结果
     */
    @GetMapping("/searchByDate")
    public Result<PageBean<Schedule>> searchByDate(
            @RequestParam String date,
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize
    ) {
        LocalDate localDate = LocalDate.parse(date);
        PageBean<Schedule> pb = scheduleService.searchByDate(localDate, pageNum, pageSize);
        return Result.success(pb);
    }
}